package com.kaishengit.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.GoodsinDao;
import com.kaishengit.dao.InventoryDao;
import com.kaishengit.pojo.Goodsin;
import com.kaishengit.pojo.Inventory;

@Service
@Transactional
public class GoodsinService {
	private GoodsinDao goodsinDao;
	private InventoryDao inventoryDao;
	public void add(Goodsin goodsin){
		Inventory inventory = inventoryDao.findByGoodsid(goodsin.getGoods().getId());
		if(inventory == null){
			Inventory inv = new Inventory();
			inv.setNumber(goodsin.getInputnum());
			inv.setGoods(goodsin.getGoods());
			
			inventoryDao.add(inv);
		}else{
			inventory.setNumber(inventory.getNumber() + goodsin.getInputnum());
			inventory.setGoods(goodsin.getGoods());
			inventoryDao.update(inventory);
		}
		goodsinDao.add(goodsin);
		
	}
	public Goodsin findById(String id){
		
		return goodsinDao.findById(id);
	}
	public List<Goodsin> findAll(){
		return goodsinDao.findAll();
	}
	public List<Goodsin> findByWhere(Goodsin goodsin){
		return goodsinDao.findByWhere(goodsin);
	}
	public void del(String id) {
		goodsinDao.del(id);
		
	}

	@Autowired
	public void setGoodsinDao(GoodsinDao goodsinDao) {
		this.goodsinDao = goodsinDao;
	}
	@Autowired
	public void setInventoryDao(InventoryDao inventoryDao) {
		this.inventoryDao = inventoryDao;
	}
	
	

}
